package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.NursingElder;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController {

    @Autowired
    private IReservationService reservationService;

    /**
     * 查询当天取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    public R<Integer> cancelreservationCount(){
        Integer data=reservationService.cancelreservationCount();//取消预约次数
        return R.ok(data);
    }

    /**
     * 查询每个时间段剩余预约次数
     * @return
     */
    @GetMapping("/countByTime")
    public R<List<TimeCountVo>> reservationCount(){
        List<TimeCountVo> data=reservationService.reservationCount();
        return R.ok(data);
    }

    /**
     * 新增预约数据
     * @param dto
     * @return
     */
    @PostMapping
    public AjaxResult saveReservation(@RequestBody ReservationDto dto){
        reservationService.saveReservation(dto);
        return success();
    }

    /**
     * 分页查询预约信息
     * @param status
     * @return
     */
    @GetMapping("/page")
    public Object pageList(@RequestParam(defaultValue = "0") Integer status) {
        startPage(); // 分页开始
        List<Reservation> list = reservationService.pageList(status);

        // 获取原分页结果（扁平结构）
        TableDataInfo tableDataInfo = getDataTable(list);

        // 构建前端需要的嵌套结构
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("total", tableDataInfo.getTotal());
        dataMap.put("rows", tableDataInfo.getRows());
        dataMap.put("code", tableDataInfo.getCode()); // 保持 200
        dataMap.put("msg", "请求成功"); // 前端要求的固定值

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);       // 最外层 code
        result.put("msg", "操作成功");  // 最外层 msg
        result.put("data", dataMap);    // 嵌套 data 字段
        return result;
    }

    @PutMapping("{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable Long id){
        reservationService.cancelReservation(id);
        return success();
    }

}